<template>
	<div id="work-bench-wrapper" v-loading.fullscreen.lock="fullscreenLoading"> </div>
</template>

<script>
import 'antd/dist/antd.less';
import { mapState } from 'vuex';
export default {
	data() {
		return {
			fullscreenLoading: false
		};
	},
	computed: {
		...mapState(['userInfo'])
	},
	mounted() {
		this.fullscreenLoading = true;
		console.log('sss', this.$route.params);
		if (this.$route.params) {
			const { data, permission } = this.$route.params;
			if (!permission || permission.length == 0 || !data) {
				debugger;
				this.$router.push({ path: '/login', params: { key: 'quit' } });
				return;
			}
			const { configure } = AgentServer.Agent;
			configure.setServiceUrl(loginUrl);
			configure.setDefaultMediaOptions(defaultMediaOptions);
			configure.setCountlyParam({ appkey: '', host: '' });
			configure.setCustomerUrl(customerUrl);
			configure.setFormUrl(formUrl);
			configure.setPublicKey(publicKey);
			configure.setResourceOriginUrlDomain(resourceOriginUrlDomain);
			configure.setResourceReplaceUrlDomain(resourceReplaceUrlDomain);
			configure.setUrlMapping(typeof urlMapping === 'undefined' ? {} : urlMapping);
			AgentServer.Agent.setLogLevel(logLevel);
			configure.setDefaultMediaOptions(defaultMediaOptions);
			configure.setMaxUploadFileSize(maxUploadFileSize);
			if (whiteboardInfo && whiteboardInfo.hostName && whiteboardInfo.whiteboardPath) {
				configure.setWhiteBoardInfo(whiteboardInfo);
			} else {
				configure.setWhiteBoardInfo({ hostName: loginUrl, whiteboardPath: '/static/whiteboard' });
			}

			if (permission.length >= 2) {
				AgentServer.Agent.init(loginUrl, {
					isCallProgress: true,
					isImagePreview: true,
					userInfoVisible: true,
					managementMenuVisible: true,
					agentManagemenMenuVisible: true,
					watermarkConfigVisible: true,
					languageConfigVisible: true,
					acceptCallConfigVisible: true,
					chatBoxVisible: true,
					layoutConfigVisible: true,
					bizPannelVisible: true,
					headerMenus: [
						{ label: '通话服务', i18nKey: 'call-service', key: 'sessionService' },
						{ label: '服务记录', i18nKey: 'service-record', key: 'serviceRecord' },
						{ label: '管理后台', i18nKey: 'management-service', key: 'managementService' },
						{ label: '在线坐席管理', i18nKey: 'online-agent-management', key: 'agentManagement' }
					], // 头部 Tab 标签
					isRecord: window.isRecord,
					allowAudioAnswerToVideoCall: true,
					qualityTestingResultVisible: true,
					deviceConfigVisible: true,
					virtualBackgroundConfigVisible: true,
					floatAlertConfigVisible: true
				});
			} else {
				AgentServer.Agent.init(loginUrl, {
					isCallProgress: true,
					isImagePreview: true,
					userInfoVisible: true,
					managementMenuVisible: true,
					agentManagemenMenuVisible: true,
					watermarkConfigVisible: true,
					languageConfigVisible: true,
					acceptCallConfigVisible: true,
					chatBoxVisible: true,
					layoutConfigVisible: true,
					bizPannelVisible: true,
					headerMenus: [
						{ label: '通话服务', i18nKey: 'call-service', key: 'sessionService' },
						{ label: '服务记录', i18nKey: 'service-record', key: 'serviceRecord' },
						{ label: '在线坐席管理', i18nKey: 'online-agent-management', key: 'agentManagement' }
					], // 头部 Tab 标签
					isRecord: window.isRecord,
					qualityTestingResultVisible: true,
					qualityTestingResultVisible: true,
					deviceConfigVisible: true,
					virtualBackgroundConfigVisible: true,
					floatAlertConfigVisible: true
				});
			}
			this.loginAms(data);
			console.log(this.$route.params);
		}

		this.$nextTick(() => {
			this.addEventListener();
			// AgentServer.Agent.addPageLeaveEvent();
			console.log(this.userInfo, 'userInfo');
		});
	},
	methods: {
		addEventListener() {
			window.addEventListener('storage', () => {
				if (!localStorage.getItem(`zco_admin_token_${window.uid}`)) {
					AgentServer.Agent.removePageLeaveEvent();
					AgentServer.destroy();
					AgentServer.Agent.hangup({ reason: 'hang up' });
					debugger;
					this.$router.push({ name: 'login', params: { key: 'quit' } });
				}
			});
			AgentServer.Agent.addEventListener(resp => {
				if (resp.event === 'DOM_EVENT') {
					console.log('DOM_EVENT:', resp.data.data);
					if (resp.data.data.target === 'enrtyManagement') {
						let routeUrl = this.$router.resolve({
							path: '/index/home?uid=' + window.uid,
							query: {}
						});
						window.open(routeUrl.href, '_blank'); //关键在此
					} else if (resp.data.data.target === 'otherPersonLogin') {
						// AgentServer.destroy();
						debugger;
						this.$router.push({ name: 'login', params: { key: 'quit' } });
					} else {
						AgentServer.destroy();
						localStorage.setItem(`zco_admin_token_${window.uid}`, '');
						debugger;
						this.$router.push({ name: 'login', params: { key: 'quit' } });
					}
				}
				if (resp.event === 'LOGIN') {
					if (resp.data.code === 10000) {
						// 登录成功，创建视图
						AgentServer.Agent.createView('work-bench-wrapper');
						AgentServer.render();
						this.fullscreenLoading = false;
					} else {
						AgentServer.destroy();
						debugger;
						this.$router.push({ name: 'login', params: { key: 'quit' } });
					}
				}
				// if (resp.event === 'ON_DROP') {
				// 	if ([401, 402, 403, 404, 405].includes(resp.data.data.code)) {
				// 		this.$router.push({ name: 'login', params: { key: 'quit' } });
				// 	}
				// }
				if (resp.event === 'LOGOUT') {
					console.log('LOGOUT');
					AgentServer.destroy();
					localStorage.setItem(`zco_admin_token_${window.uid}`, '');
					debugger;
					this.$router.push({ name: 'login', params: { key: 'quit' } });
				}
				// console.log(resp);
			});
		},
		async loginAms(data) {
			try {
				AgentServer.Agent.ssologinSystem(
					data,
					resp => {
						console.log(resp, 'success');
					},
					error => {
						debugger;
						this.$router.push({ name: 'login', params: { key: 'quit' } });
						console.log(error, 'error');
					}
				);
			} catch (error) {
				console.error(error);
				AgentServer.destroy();
				debugger;
				this.$router.push({ name: 'login', params: { key: 'quit' } });
			}
		}
	},
	beforeDestroy() {
		AgentServer.Agent.removeEventListener();
		AgentServer.Agent.removePageLeaveEvent();
	}
};
</script>

<style scoped>
#work-bench-wrapper {
	width: 100%;
	height: 100%;
}
/* @import url('../../static/css/widget-main.css'); */
</style>
